import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ext/home/api/Api.dart';
import 'package:flutter_ext/home/ext/data.dart';
import 'package:flutter_ext/home/ext/http.dart';
import 'package:flutter_ext/home/ext/constant.dart' as ext;

class FeedList extends StatefulWidget{

  String feedType;


  FeedList({Key key,this.feedType}):super(key: key);

  @override
  State<StatefulWidget> createState() => new _FeedListState();

}

class _FeedListState extends State<FeedList> {
  var dataList;
  var curPage = 1;
  var listTotalSize = 0;
  ScrollController _controller = new ScrollController();

  _FeedListState(){
    _controller.addListener((){
      var maxScroll = _controller.position.maxScrollExtent;
      var pixels = _controller.position.pixels;
      if(maxScroll == pixels){
        print('load more ...');
        curPage++;
        getNewsList(true);
      }
    });
  }

  @override
  void initState() {
    super.initState();
    getNewsList(false);
  }

  @override
  Widget build(BuildContext context) {
    print('FeedList==> $dataList');
    if(dataList == null){
      return new Center(
        child: new CupertinoActivityIndicator(),
      );
    }else{
      Widget listView = _buildListView(context,dataList);
      return new RefreshIndicator(child: listView, onRefresh: _pullToRefresh);
    }
  }

  getNewsList(bool isLoadMore) {
    var url = Api.FEED_URL;
    url += widget.feedType +'/10/'+this.curPage.toString();
    print('feedListUrl: $url');
    HttpExt.get(url,(data){
      if(data != null){
        CategoryResponse categoryResponse =
        CategoryResponse.fromJson(jsonDecode(data));
        if(!categoryResponse.error){
          var _dataList = categoryResponse.results;
          print(_dataList);
          if(_dataList.length>0){
            setState(() {
              if(!isLoadMore){
                dataList = _dataList;
              }else{
                List list = new List();
                list.addAll(dataList);
                list.addAll(_dataList);
                dataList = list;
              }
            });
          }
        }
      }
    },(e){
      print('get news list error: $e');
    });
  }

  Widget _buildListView(BuildContext context, List results) {
    print('result==> $results');
    switch(results.length){
      case 1:
        return new Center(
          child: new Card(
            elevation: 16.0,
            child: new Text('-no data to show-'),
          ),
        );
      default:
        return new ListView.builder(
            physics: const AlwaysScrollableScrollPhysics(),
            padding: const EdgeInsets.all(2.0),
            controller: _controller,
            itemCount: results == null? 0:results.length,
            itemBuilder: (context,i){
              return ext.buildRow(context,results[i]);
            });
    }
  }

  Future<Null> _pullToRefresh() async{
    curPage = 1;
    getNewsList(false);
    return null;
  }
}
